package com.huitone.gddw.service.impl.monitoring.base;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.ItemDef;
import com.huitone.gddw.dao.entity.ObjItem;
import com.huitone.gddw.dao.monitoring.IItemDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.base.ItemDefDTO;
import com.huitone.gddw.service.monitoring.base.IHistoryIntService;
import com.huitone.gddw.service.monitoring.base.IHistoryService;
import com.huitone.gddw.service.monitoring.base.IHistoryStrService;
import com.huitone.gddw.service.monitoring.base.IInnormalEventService;
import com.huitone.gddw.service.monitoring.base.IItemDefService;
import com.huitone.gddw.service.monitoring.base.IItemKeyDefService;
import com.huitone.gddw.service.monitoring.base.ITrendsIntService;
import com.huitone.gddw.service.monitoring.base.ITrendsService;
import com.huitone.gddw.service.monitoring.base.ITriggerService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 对象子项 接口实现类
 * @author wuluodan
 *
 */
@Service
public class ItemDefService implements IItemDefService {

	@Autowired
	private IItemDefDao itemDefDao;
	
	@Autowired
	private IItemKeyDefService itemKeyDefService;
	@Autowired
	private IHistoryService historyService;
	@Autowired
	private IHistoryIntService historyIntService;
	@Autowired
	private IHistoryStrService historyStrService;
	@Autowired
	private ITriggerService triggerService;
	@Autowired
	private IInnormalEventService innormalEventService;
	@Autowired
	private ITrendsService trendsService;
	@Autowired
	private ITrendsIntService trendsIntService;
	
	@Override
	public QueryPage<ItemDefDTO> selectPage(QueryPage<ItemDef> page) {
		Map<String, Object> params = page.getParams();
		params.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		params.put("end", page.getPageNo() * page.getPageSize());
		
		List<ItemDefDTO> result = itemDefDao.selectPage(params);
		int totalCount = itemDefDao.selectCount(params);
		
		QueryPage<ItemDefDTO> queryPage = new QueryPage<ItemDefDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	@Override
	public ItemDef getById(String itemId) {
		return itemDefDao.getById(itemId);
	}
	
	@Override
	public List<ObjItem> getByObjId(String objId) {
		return itemDefDao.getByObjId(objId);
	}
	
	@Override
	public ItemDefDTO findOne(String itemId) {
		return itemDefDao.findOne(itemId);
	}
	
	@Override
	public void save(ItemDef entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		itemDefDao.save(entity);
	}
	
	@Override
	public void update(ItemDef entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		itemDefDao.update(entity);
	}
	
	@Override
	public void delete(String itemId) {
		// 删除监控数据趋势表信息
		trendsService.delByItemId(itemId);
		trendsIntService.delByItemId(itemId);
		
		// 监控异常事件信息
		innormalEventService.delByItemId(itemId);
		// 删除监控预警信息
		triggerService.delByItemId(itemId);
		
		// 删除监控数据表信息
		historyService.delByItemId(itemId);
		historyIntService.delByItemId(itemId);
		historyStrService.delByItemId(itemId);
		// 删除属性信息
		itemKeyDefService.delByItemId(itemId);
		
		// 删除子项信息
		itemDefDao.delByItemId(itemId);
	}

	@Override
	public void delByObjId(String objId) {
		itemDefDao.delByObjId(objId);
	}
	
}
